﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IrrKlang;
using System.IO;

namespace NightcoreMaker
{
    public partial class MainForm : Form
    {
        ISound musicCurrentlyPlayed = null;
        ISoundEngine engine;
        public MainForm()
        {
            //I declare the engine here so i can use it anywhere else in the class
            engine = new ISoundEngine();
            InitializeComponent();
            
        }

        //On click show explorer and play the selected file
        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            if(musicCurrentlyPlayed != null)
            {
                engine.StopAllSounds(); //Stops the music currently playing before selecting another one
            }
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Music Files|*.mp3;*.wav;*.ogg;*.flac"; //Filter the explorer dialog so it only show supported music files
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                musicCurrentlyPlayed = engine.Play2D(dialog.FileName);
                songNameLabel.Text = Path.GetFileNameWithoutExtension(dialog.FileName); //Only show the file name new to the "Open Music File" button
                playButton.Text = "Pause";
            }
            
        }

        private void pitchBar_Scroll(object sender, EventArgs e)
        {
            if (musicCurrentlyPlayed != null)
            {
                //Changes the pitch of the song when scroll
                double newPitch = ((double.Parse(pitchBar.Value + "") + double.Parse(pitchBar.Maximum + "")) / double.Parse(pitchBar.Maximum * 2 + "")) * 1.5;
                musicCurrentlyPlayed.PlaybackSpeed = (float)newPitch;
            }
        }

        private void volumeBar_Scroll(object sender, EventArgs e)
        {
            if(musicCurrentlyPlayed != null)
            {
                //Changes the volume of the song when scroll
                double newVolume = double.Parse(volumeBar.Value + "") / double.Parse(volumeBar.Maximum + "");
                musicCurrentlyPlayed.Volume = (float)newVolume;
            }
        }

        private void panBar_Scroll(object sender, EventArgs e)
        {
            if (musicCurrentlyPlayed != null)
            {
                //Changes the sound panorama of the song when scroll
                double newPan = double.Parse(panBar.Value + "") / double.Parse(panBar.Maximum + "") * -1;
                musicCurrentlyPlayed.Pan = (float)newPan;
            }
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            //Play/Pause Button. You know what that does, don't you? I shouldn't even explain it.
            if(musicCurrentlyPlayed != null)
            {
                if (!musicCurrentlyPlayed.Paused)
                {
                    musicCurrentlyPlayed.Paused = true;
                    playButton.Text = "Play";
                    Debug.WriteLine("saaaaaaaufrf");
                    return;
                }
                if (musicCurrentlyPlayed.Paused)
                {
                    musicCurrentlyPlayed.Paused = false;
                    playButton.Text = "Pause";
                    return;
                }
            }
        }

    }
}
